




//
//  MBBTNTableview.m
//  tableview多按钮选择
//
//  Created by MB__Lee on 2018/5/17.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBBTNTableview.h"
#import "MBBTNCell.h"

#import "MBBTBFootView.h"

#define Colum 4  //列数
#define TopLabelHeight 44

@interface MBBTNTableview()<UITableViewDelegate,UITableViewDataSource,MBBTBFootViewDelegate>
@property(nonatomic,strong)MBBTBFootView *footView;

@property(nonatomic,strong)NSMutableArray <NSIndexPath *>*indexPathMut;//用来记录选中的 indexpath
@property(nonatomic,strong)NSIndexPath *oneIndexpath;
@property(nonatomic,strong)NSIndexPath *twoIndexpath;
@property(nonatomic,strong)NSIndexPath *threeIndexpath;
@property(nonatomic,strong)NSIndexPath *fourIndexpath;


@end

static NSString *MBBTNCell_Identi = @"MBBTNCell";
static NSString *MBBTBFootView_Identi = @"MBBTBFootView";
@implementation MBBTNTableview
-(NSMutableArray<NSIndexPath *> *)indexPathMut{
    if (!_indexPathMut) {
        _indexPathMut = [NSMutableArray array];
    }
    return  _indexPathMut;
}


-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self registerNib:[UINib nibWithNibName:MBBTNCell_Identi bundle:nil] forCellReuseIdentifier:MBBTNCell_Identi];
        
        

        
        self.tableFooterView = self.footView;
    
       
    }
    return  self;
}

-(void)setMut:(NSMutableArray *)mut{
    _mut = mut;
    [self reloadData];
}
-(void)setTopLabelMut:(NSMutableArray *)topLabelMut{
    _topLabelMut = topLabelMut;
    [self reloadData];
}
-(void)insertSource{
    
}

-(MBBTBFootView *)footView{
    if (!_footView) {
        _footView = [[NSBundle mainBundle] loadNibNamed:MBBTBFootView_Identi owner:nil options:nil].lastObject;
        _footView.frame = CGRectMake(0, 0, self.bounds.size.width, MBScale(60));
        _footView.delegate = self;
        
    }
    return  _footView;
}

#pragma mark ---delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  self.mut.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MBBTNCell *cell = [tableView dequeueReusableCellWithIdentifier:MBBTNCell_Identi];
    cell.cellMut = self.mut[indexPath.section];
    cell.topLabelName = self.topLabelMut[indexPath.section];
    cell.block = ^(NSInteger row) {
        
        if (indexPath.section == 0) {
            _oneIndexpath  = [NSIndexPath indexPathForRow:row inSection:indexPath.section];
            
            
        }else if (indexPath.section == 1){
             _twoIndexpath  = [NSIndexPath indexPathForRow:row inSection:indexPath.section];
            
        }else if (indexPath.section == 2){
            _threeIndexpath = [NSIndexPath indexPathForRow:row inSection:indexPath.section];
        }else if (indexPath.section == 3){
            _fourIndexpath = [NSIndexPath indexPathForRow:row inSection:indexPath.section];

        }
        
       
        
       
    };
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat num = [self.mut[indexPath.section] count];
    num += (Colum - 1);
    NSInteger rows = num / Colum;
    //通过计算按钮的个数来算高度 ，最后加上cell 顶部的label 高度
    return  rows * (itemHeight + Margin)  + TopLabelHeight;
    
    
}

#pragma mark ---MBBTBFootViewDelegate代理方法
-(void)cancelBTNClik{
    [self resetIndexPath];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"resetBTN" object:nil];
    
    if ([self.BTNDelegate respondsToSelector:@selector(MBBTNTableviewClearInfo:)]) {
        [self.BTNDelegate MBBTNTableviewClearInfo:self];
    }
    
}
-(void)sureBTNClik{

    
    //1.给indexpath 数据添加对象，如果总数为空，直接从父控制器上移除
    if (_oneIndexpath != nil) {
        [self.indexPathMut addObject:_oneIndexpath];
    }
    if (_twoIndexpath != nil) {
        [self.indexPathMut addObject:_twoIndexpath];
    }
    if (_threeIndexpath != nil) {
        [self.indexPathMut addObject:_threeIndexpath];
    }
    if (_fourIndexpath != nil) {
        [self.indexPathMut addObject:_fourIndexpath];
    }
    
//    if (self.indexPathMut.count == 0) { // 数组为空直接隐藏，不做任何事情
//        [self removeFromSuperview];
//        return;
//    }
    //2.不为空的话，将数据传递给代理快让代理拿值
    if ([self.BTNDelegate respondsToSelector:@selector(MBBTNTableview:sendIndexPathArray:)]) {
        

        [self.BTNDelegate MBBTNTableview:self sendIndexPathArray:self.indexPathMut];
//        [self resetIndexPath];
        [self.indexPathMut removeAllObjects];
        
        
    }
}


//1、移除数组中的indexpath  2、将原来记录的indexpath 置 nil；
-(void)resetIndexPath{
    [self.indexPathMut removeAllObjects];
    _oneIndexpath = nil;
    _twoIndexpath = nil;
    _threeIndexpath = nil;
    _fourIndexpath = nil;
    
}








@end
